package com.ten_4_4.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ten_4_4.hiring.comm.util.AppUtil;
import com.ten_4_4.hiring.company.dao.PositionDao;
import com.ten_4_4.hiring.company.po.PositionPo;

/**
 * Position Servlet
 *
 * @author Zzy.
 * @since 2016年1月14日
 */
public class PositionServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private PositionDao positionDao = new PositionDao();
	//private ApplicationDao applicationDao = new ApplicationDao();
	//private ResumeDao resumeDao = new ResumeDao();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("add".equals(command)) {
			add(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("toPositionAdd".equals(command)) {
			toPositionAdd (request, response);
		} else if ("list".equals(command)) {
			list(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("preupdate".equals(command)) {
			preupdate(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}


	/**
	 * 所有招聘职位的列举
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、调用前台传来的参数
		// 2、调用业务逻辑
		List<PositionPo> positionList = positionDao.list();
		// 3、转向
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("WEB-INF/view/company/position-list.jsp").forward(
				request, response);
	}


//	private void detail(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		if (!AppUtil.isLogin(request)) {
//			request.setAttribute("sessionClosed", "请先登录，再执行简历投递！");
//			request.getRequestDispatcher("view/talent/talent-login.jsp")
//					.forward(request, response);
//		} else {
//			// 1、调用前台传来的参数
//			int id = Integer.parseInt(request.getParameter("id"));
//			// 2、调用业务逻辑
//			PositionPo position = positionDao.detail(id);
//			int talentId = AppUtil.getTalentId(request, response);
//			ApplicationEntity application = applicationDao.isApplied(talentId,
//					id);
//			List<ResumeEntity> resumeList = resumeDao.list(talentId);
//			// 3、转向
//			request.setAttribute("position", position);
//			request.setAttribute("application", application);
//			request.setAttribute("resumeList", resumeList);
//			request.getRequestDispatcher("view/company/position-detail.jsp")
//					.forward(request, response);
//		}
//	}

	/**
	 * 职位删除
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		// 2、调用业务逻辑
		int row = positionDao.delete(positionId);
		// 3、转向
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	
	private void toPositionAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/view/company/position-add.jsp").forward(
				request, response);
	}
	/**
	 * 发布职位
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int companyId = AppUtil.getCompanyId(request, response);
		String name = request.getParameter("name");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");
		int education = Integer.parseInt(request.getParameter("education"));
		int workTime1 = Integer.parseInt(request.getParameter("workTime1"));
		int workTime2 = Integer.parseInt(request.getParameter("workTime2"));

		// 2、调用业务逻辑
		PositionPo position = new PositionPo();
		position.setCompanyId(companyId);
		position.setName(name);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		position.setDuty(duty);
		position.setAbility(ability);
		position.setEducation(education);
		position.setWorkTime1(workTime1);
		position.setWorkTime2(workTime2);
		int row = positionDao.add(position);

		// 3、转向
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}

	/**
	 * 发布职位后后台主页显示
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	 
	private void main(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		int row = Integer.parseInt(request.getParameter("row"));
		int companyId = AppUtil.getCompanyId(request, response);
		
		if(row == 1){
			request.setAttribute("success", "职位发布成功！");
		}else{
			request.setAttribute("error", "职位发布失败！");		
		}
		List<PositionPo> positionList = PositionDao.listById(companyId);
		request.setAttribute("positionList", positionList);
		request.getRequestDispatcher("WEB-INF/view/company/company-homepage.jsp").forward(request, response);
	}
	
	
	/**
	 * 职位修改（查询之前信息，显示到修改页面，方便后期参照修改）
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void preupdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2、调用业务逻辑
		PositionPo position = positionDao.get(id);
		// 3、转向
		if (position == null) {
			request.setAttribute("error", "该职位不存在~");
			int companyId = AppUtil.getCompanyId(request, response);
			List<PositionPo> positionList = positionDao.listById(companyId);
			request.setAttribute("positionList", positionList);
			request.getRequestDispatcher("WEB-INF/view/company/company-homepage.jsp").forward(
					request, response);
		} else {
			request.setAttribute("position", position);
			request.getRequestDispatcher("WEB-INF/view/company/position-update.jsp")
					.forward(request, response);
		}
	}

	/**
	 * 职位修改（获取修改信息，执行修改）
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取前台传过来的参数
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		int salary1 = Integer.parseInt(request.getParameter("salary1"));
		int salary2 = Integer.parseInt(request.getParameter("salary2"));
		String duty = request.getParameter("duty");
		String ability = request.getParameter("ability");

		// 2、调用业务逻辑
		PositionPo position = new PositionPo();
		position.setId(id);
		position.setName(name);
		position.setSalary1(salary1);
		position.setSalary2(salary2);
		position.setDuty(duty);
		position.setAbility(ability);
		int row = positionDao.update(position);

		// 3、转向
		response.sendRedirect("CompanyServlet?command=main&row=" + row);
	}
}
